#include <stdio.h>
#include <stdlib.h>
#include "Settings.h"


Settings::Settings() {

	forward = 'w';
	reverse = 's';
	left = 'a';
	right = 'd';
	up = 'q';
	down = 'z';
	fire = ' ';

}

Settings::Settings(FILE * fp) {

	get_keys( fp );

}

void Settings::get_keys( FILE * fp ) {
	char s[128];
	char c = '0';
	int i = 0;

	while ( c != EOF && c != '\0' ) {
		c = fgetc(fp);
		i = 0;
		while ( c != '\0' && c != '\n'&&  c != EOF ) {
	
			s[i] = c;
			i++;
			c = fgetc(fp);
		}
		if(s[i] == 0) continue;
		s[i]='\0';
		if(strFound("reverse",s)) 	forward = s[0];
		else if(strFound("forward",s)) 	forward = s[0];
		else if(strFound("down",s)) 	forward = s[0];
		else if(strFound("up",s)) 	forward = s[0];
		else if(strFound("left",s)) 	forward = s[0];
		else if(strFound("right",s)) 	forward = s[0];
		else if(strFound("fire",s)) 	forward = s[0];
	}
}


int Settings::strFound(const char* s1, char* s2)
{
int i = 0; int j = 0;
while(s2[i]!='\0')
{

	if(s2[i]==s1[0]){
	j=0;
	while(s1[j]!='\0')
	{
		if(s2[i+j] != s1[j])
			break;
	}}
	if(s1[j]=='\0')
		return 1;
	else j=0;
}
return 0;
}
